package com.example.basic.utils.aop;

import com.alibaba.fastjson.JSON;
import com.example.basic.core.result.Result;
import com.example.basic.utils.repeatsubmit.RepeatSubmit;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
* 处理和包装异常
*
* @author
*/
@Aspect
@Component
public class ControllerAOP {
   private static final Logger logger = LoggerFactory.getLogger(ControllerAOP.class);

   @Autowired
   private RepeatSubmit repeatSubmit;

   @Pointcut("execution(* com.example.basic.web.controller..*(..))")
   public void controllerMethod(){}

   @Around("controllerMethod()")
   public Object handlerControllerMethod(ProceedingJoinPoint pjp) throws Throwable {

      Object []  params = pjp.getArgs();
      if(params != null && params.length > 0){
         logger.debug("接口请求地址：	"+pjp.toLongString());
         logger.debug("参数如下:");
         for (Object o :params){
            if(o != null ){
               try {
                  logger.debug(JSON.toJSONString(o));
               }catch (Exception e){
               }
            }
         }
      }
      /**
       * 防重复提交
       */
      repeatSubmit.repeatSubmit(pjp,params);
      Result<?> result = (Result<?>) pjp.proceed();
      return result;
   }

}
